package com.hqd.ch03.v51.boot.autoconfigure.web.servlet;

import com.hqd.ch03.v51.boot.web.servlet.ServletContextInitializer;
import com.hqd.ch03.v51.core.Ordered;
import com.hqd.ch03.v51.utils.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

public abstract class RegistrationBean implements ServletContextInitializer, Ordered {

    private static final Log logger = LogFactory.getLog(RegistrationBean.class);

    private int order = Ordered.LOWEST_PRECEDENCE;

    private boolean enabled = true;

    @Override
    public final void onStartup(ServletContext servletContext) throws ServletException {
        String description = getDescription();
        if (!isEnabled()) {
            logger.info(StringUtils.capitalize(description) + " was not registered (disabled)");
            return;
        }
        register(description, servletContext);
    }

    /**
     * Return a description of the registration. For example "Servlet resourceServlet"
     *
     * @return a description of the registration
     */
    protected abstract String getDescription();

    /**
     * Register this bean with the servlet context.
     *
     * @param description    a description of the item being registered
     * @param servletContext the servlet context
     */
    protected abstract void register(String description, ServletContext servletContext);

    /**
     * Return if the registration is enabled.
     *
     * @return if enabled (default {@code true})
     */
    public boolean isEnabled() {
        return this.enabled;
    }

    /**
     * Flag to indicate that the registration is enabled.
     *
     * @param enabled the enabled to set
     */
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    /**
     * Get the order of the registration bean.
     *
     * @return the order
     */
    @Override
    public int getOrder() {
        return this.order;
    }

    /**
     * Set the order of the registration bean.
     *
     * @param order the order
     */
    public void setOrder(int order) {
        this.order = order;
    }

}
